package edu.tec.mty.test.gui.canvas;

import java.util.Vector;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import edu.tec.mty.core.*;
import edu.tec.mty.core.modules.*;
import edu.tec.mty.core.modules.models.*;
//import gameStates.playerBehaviors.controller.*;
//import gameStates.playerBehaviors.controller.information.modules.models.*;

public class RC2DCanvas extends Component {

	private int width;
	private int height;
	private boolean isSetFrame;
	private UFrame frame;
	private Graphics g;
	private boolean drawPlayer;
	private boolean drawPlayers;
	private boolean drawBall;
	//private WorldModelOriginal worldModel;
	private Self player;
	private Controller controller;
	private Game game;
	private Commands commands;
	
	public RC2DCanvas() {
		this.width = 595;
		this.height = 410;
		this.isSetFrame = false;
		this.drawBall = false;
		this.drawPlayer = false;
		this.drawPlayers = false;
	}
	
	public void setFrame(UFrame frame) {
		this.frame = frame;
		this.isSetFrame = true;
	}
	
	public void toggleDrawBall() {
		this.drawBall = !this.drawBall;
	}
	
	public void toggleDrawPlayers() {
		this.drawPlayers = !this.drawPlayers;
	}
	
	public void toggleDrawPlayer() {
		this.drawPlayer = !this.drawPlayer;
	}
	
	public void setController(Controller controller) {
		this.controller = controller;
		try {
			//this.worldModel = controller.getReferenceToWorldModel();
			this.player = controller.getReferenceToPlayer();
		} catch (Exception e) {
			
		}
	}
	
	public Dimension getPreferredSize() {
		return new Dimension(this.width, this.height);
	}
	
	private void drawPlayer(double size, double x, double y, double direction, Color color) {
		this.drawCircle(x, y, size, Color.BLACK, color);
		this.drawVector(x, y, direction, size, Color.BLACK);
	}
	
	public void drawRectangle(double x0, double y0, double x1, double y1, Color lineColor) {
		int x = CanvasUtil.convertXPositionToCoord(x0);
		int y = CanvasUtil.convertYPositionToCoord(y0);
		int width = CanvasUtil.convertXPositionToCoord(x1) - x;
		int height = CanvasUtil.convertYPositionToCoord(y1) - y;
		this.g.setColor(lineColor);
		this.g.drawRect(x, y, width, height);
	}
	
	public void drawRectangle(double x0, double y0, double x1, double y1, Color lineColor, Color fillColor) {
		int x = CanvasUtil.convertXPositionToCoord(x0);
		int y = CanvasUtil.convertYPositionToCoord(y0);
		int width = CanvasUtil.convertXPositionToCoord(x1) - x;
		int height = CanvasUtil.convertYPositionToCoord(y1) - y;
		this.g.setColor(lineColor);
		this.g.drawRect(x, y, width, height);
		this.g.setColor(fillColor);
		this.g.fillRect(x, y, width, height);
	}
	
	public void drawVector(double x, double y, double direction, double length, Color color) {
		int x0 = CanvasUtil.convertXPositionToCoord(x);
		int y0 = CanvasUtil.convertYPositionToCoord(y);
		int x1 = x0 + CanvasUtil.getDeltaX(direction, length);
		int y1 = y0 + CanvasUtil.getDeltaY(direction, length);
		this.g.setColor(color);
		this.g.drawLine(x0, y0, x1, y1);
	}
	
	public void drawLine(double x0, double y0, double x1, double y1, Color color) {
		int xo = CanvasUtil.convertXPositionToCoord(x0);
		int yo = CanvasUtil.convertYPositionToCoord(y0);
		int xf = CanvasUtil.convertXPositionToCoord(x1);
		int yf = CanvasUtil.convertYPositionToCoord(y1);
		this.g.setColor(color);
		this.g.drawLine(xo, yo, xf, yf);
	}
	
	public void drawCircle(double x, double y, double r, Color lineColor) {
		int x0 = CanvasUtil.convertXPositionToCoord(x) - CanvasUtil.convertLength(r);
		int y0 = CanvasUtil.convertYPositionToCoord(y) - CanvasUtil.convertLength(r);
		int width = 2*CanvasUtil.convertLength(r);
		int height = 2*CanvasUtil.convertLength(r);
		this.g.setColor(lineColor);
		this.g.drawOval(x0, y0, width, height);
	}
	
	public void drawCircle(double x, double y, double r, Color lineColor, Color fillColor) {
		int x0 = CanvasUtil.convertXPositionToCoord(x) - CanvasUtil.convertLength(r);
		int y0 = CanvasUtil.convertYPositionToCoord(y) - CanvasUtil.convertLength(r);
		int width = 2*CanvasUtil.convertLength(r);
		int height = 2*CanvasUtil.convertLength(r);
		this.g.setColor(lineColor);
		this.g.drawOval(x0, y0, width, height);
		this.g.setColor(fillColor);
		this.g.fillOval(x0 + 1, y0 + 1, width - 2, height - 2);
	}
	
	private void drawField() {
		this.drawRectangle(-59.5, -41.0, 59.5, 41.0, new Color(31,160,31), new Color(31,160,31));
		this.drawRectangle(-52.5, -34.0, 52.5, 34.0, Color.WHITE);
		this.drawLine(0.0, -34.0, 0.0, 34.0, Color.WHITE);
		this.drawCircle(0.0, 0.0, 9.0, Color.WHITE);
	}
	
	public void paint(Graphics g) {
		this.g = g;
		this.drawField();
		if (this.isSetFrame) {
			this.frame.onFrame(this, this.controller);
		}
		if (this.drawBall) {
			/*BallModel ballModel = this.worldModel.getBallModel();
			if (ballModel.isInitiated()) {
				Position ballPosition = ballModel.getPosition();
				this.drawCircle(ballPosition.getX(), ballPosition.getY(), 1.0, Color.BLACK, Color.BLUE);
			}*/
		}
		if (this.drawPlayer) {
			Position position = this.player.getPosition();//this.controller.getPosition();
			this.drawPlayer(1.3, position.getX(), position.getY(), position.getBodyAngle(), Color.YELLOW);
		}
		if (this.drawPlayers) {
			/*Players players = this.worldModel.getWorldModel();
			Vector<MemberModel> memberList = players.getMemberList();
			for (int i = 0; i < memberList.size(); i++) {
				Position position = memberList.elementAt(i).getPosition();
				if (memberList.elementAt(i).isInRange()) {
					this.drawPlayer(1.3, position.getX(), position.getY(), position.getBodyAngle(), Color.GREEN);
				} else {
					this.drawPlayer(1.3, position.getX(), position.getY(), position.getBodyAngle(), Color.GRAY);
				}
			}*/
		}
	}
	
}
